package Juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:34398
 * Date:2024-08-03
 * Time:14:28
 */
public class CallableTest {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //通过匿名内部类创建对象
        Runnable runnable=new Runnable() {
            @Override
            public void run() {

            }
        };



        Callable<Integer> callable=new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("hehhe");
                return 10;
            }
        };

        /**在创建线程的时候，多出了创建FutureTask的对象这一步*/
        FutureTask<Integer> futureTask=new FutureTask<>(callable);

        Thread thread=new Thread(futureTask);
        thread.start();
        thread.join();

        /*get方法可以获取到返回值*/
        int ret=futureTask.get();
        System.out.println("thread:"+ret);
    }
}
